package main

import (
	"crypto/md5"
	"dlib/dbus"
	"fmt"
)

type PinyinTrie struct{}

const (
	_PINYIN_TRIE_DEST = "com.deepin.daemon.PinyinTrie"
	_PINYIN_TRIE_PATH = "/com/deepin/daemon/PinyinTrie"
	_PINYIN_TRIE_IFC  = "com.deepin.daemon.PinyinTrie"
)

var (
	trieMD5 map[[16]byte]*Trie
	strsMD5 map[[16]byte][]string
)

func (ptrie *PinyinTrie) GetDBusInfo() dbus.DBusInfo {
	return dbus.DBusInfo{
		_PINYIN_TRIE_DEST,
		_PINYIN_TRIE_PATH,
		_PINYIN_TRIE_IFC}
}

func (ptrie *PinyinTrie) NewTrieWithString(values []string) [16]byte {
	root := NewTrie()
	if values == nil {
		return [16]byte{}
	}

	md5Byte := md5.Sum([]byte(GetStringFromArray(values)))
	strsMD5[md5Byte] = values
	root.InsertStringArray(values)
	trieMD5[md5Byte] = root
	fmt.Println(md5Byte)
	return md5Byte
}

func GetStringFromArray(strs []string) string {
	str := ""

	for i, _ := range strs {
		str += strs[i]
	}

	return str
}

func (ptrie *PinyinTrie) TraversalTrie(strByte [16]byte) {
	root := trieMD5[strByte]
	root.TraversalTrie()
}

func (ptrie *PinyinTrie) SearchTrieWithString(keys string,
	strByte [16]byte) []string {
	root := trieMD5[strByte]
	rets := root.SearchTrie(keys)

	retStrs := []string{}
	strArray := strsMD5[strByte]
	for _, v := range rets {
		retStrs = append(retStrs, strArray[v])
	}

	return retStrs
}

func main() {
	strs := []string{"hello", "heorld"}
	trieMD5 = make(map[[16]byte]*Trie)
	strsMD5 = make(map[[16]byte][]string)
	ptrie := PinyinTrie{}
	ptrie.NewTrieWithString(strs)
	dbus.InstallOnSession(&ptrie)
	md5Byte := md5.Sum([]byte(GetStringFromArray(strs)))
	ptrie.TraversalTrie(md5Byte)
	fmt.Println(ptrie.SearchTrieWithString("he", md5Byte))
	select {}
}
